﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SnakeGame
{
    class Snake
    {
        Direction direction;
        Point head = new Point();
        List<Point> body = new List<Point>();

        public Direction Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        public Snake(Point p)
        {
            this.head = new Point(p.X, p.Y);
            this.body = new List<Point> { new Point(p.X - 1, p.Y), new Point(p.X - 2, p.Y) };
        }

        public Point Head
        {
            get { return this.head; }
        }

        public List<Point> Body
        {
            get { return this.body; }
        }

        public void ChangePosition()
        {
            Point[] temp = new Point[2];
            temp[0] = this.Body[0];
            this.Body[0] = new Point(this.Head.X, this.Head.Y);
            for (int i = 1; i < this.Body.Count; i++)
            {
                temp[1] = this.Body[i];
                this.Body[i] = temp[0];
                temp[0] = temp[1];
            }

            switch (this.direction)
            {
                case Direction.Up:
                    this.Head.Y--;
                    break;

                case Direction.Right:
                    this.Head.X++;
                    break;

                case Direction.Down:
                    this.Head.Y++;
                    break;

                case Direction.Left:
                    this.Head.X--;
                    break;
            }

        }

        public bool isIntersection(Point p)
        {
            if (this.Head.Equals(p) || this.Body.Contains(p)) { return true; }
            return false;
        }

        public bool Growing(Point Eat)
        {
            int lastInSnakeBody = this.body.Count - 1;
            bool eating = false;
            if (this.Head.Equals(Eat))
            {
                if (this.body[lastInSnakeBody].X == this.body[lastInSnakeBody - 1].X)
                {
                    this.body.Add(new Point(this.body[lastInSnakeBody].X, this.body[lastInSnakeBody].Y + 1));
                }
                else
                {
                    this.body.Add(new Point(this.body[lastInSnakeBody].X + 1, this.body[lastInSnakeBody].Y));
                }
                eating = true;
            }
            return eating;
        }

        public bool isIntersectionWithField(List<Point> line)
        {
            if (line.Contains(this.Head)) { return true; }
            return false;
        }

        public bool SelfEating()
        {
            if (this.Body.Contains(this.Head)) { return true; }
            return false;
        }
    }

}
